home *** CD-ROM | disk | FTP | other *** search
- /*****
- *
- * PrefDialog.c
- *
- * This is a support file for "Grant's CGI Framework".
- * Please see the license agreement that accompanies the distribution package
- * for licensing details.
- *
- * Copyright ©1996 by Grant Neufeld
- * grant@acm.com
- * http://arpp.carleton.ca/cgi/framework/
- *
- *****/
-
- /***
- * Warning: if you modify the dialog to use non-numeric text entry fields,
- * you'll have to modify 'prefDialogEventFilter' to not trap non-numeric
- * characters.
- ***/
-
- #include "MyConfiguration.h"
- #if kCompileWithPreferencesDialog
-
- #include <Dialogs.h>
- #include <OSUtils.h>
- #include <Quickdraw.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- //#include "constants.h"
- #include "globals.h"
-
- //#include "CGI.h"
- //#include "DebugUtil.h"
- #include "DialogFunc.h"
- #include "EventUtil.h"
- //#include "FileUtil.h"
- //#include "MemoryUtil.h"
- #include "PrefUtil.h"
- #include "ProcessUtil.h"
-
- #include "PrefDialog.h"
-
-
- /*** CONSTANT DECLARATIONS ***/
-
- #define krPrefsDialog 500
-
- #define kriSleepTicks 7
- #define kriSleepTicksBusy 8
- #define kriIdleTimeBeforeQuit 9
-
-
- /*** LOCAL VARIABLES ***/
-
-
-
- /*** LOCAL PROTOTYPES ***/
-
- pascal Boolean prefDialogEventFilter ( DialogPtr, EventRecord *, short * );
-
-
- /*** FUNCTIONS ***/
-
- /* Bring up the preferences dialog */
- void
- PrefDialog ( void )
- {
- GrafPtr savePort;
- DialogPtr theDialog;
- UInt32 sleepTicks;
- char tempString[256];
- short itemType;
- Handle item;
- Rect box;
- UserItemUPP defaultButtonOutlineUPP;
- ModalFilterUPP dialogEventFilterUPP;
- short itemHit;
- Boolean dialogNotDone;
- Boolean dialogCancelled;
- WindowPtr dialogWindow;
- char sleepTicksString[256];
- char sleepTicksBusyString[256];
- char idleTimeQuitString[256];
-
- ProcessWaitUntilFront ( kProcessNoTimeout );
-
- GetPort ( &savePort ); /* save the current port, whatever it is */
-
- /* get dialog ptr from resource file */
- theDialog = GetNewDialog ( krPrefsDialog, NULL, (WindowPtr)-1L );
- if ( theDialog == NULL )
- {
- SysBeep ( 30 );
-
- return;
- }
-
- /* Sleep Ticks */
- sleepTicks = ProcessSleepGetDefault ();
- sprintf ( tempString, "%d", sleepTicks );
- C2PStr ( tempString );
- GetDialogItem ( theDialog, kriSleepTicks, &itemType, &item, &box );
- SetDialogItemText ( item, (StringPtr)tempString );
- SelectDialogItemText ( theDialog, kriSleepTicks, nil, 32767 );
-
- /* Sleep Ticks Busy */
- sleepTicks = ProcessSleepGetBusy ();
- sprintf ( tempString, "%d", sleepTicks );
- C2PStr ( tempString );
- GetDialogItem ( theDialog, kriSleepTicksBusy, &itemType, &item, &box );
- SetDialogItemText ( item, (StringPtr)tempString );
-
- /* Idle Time to Quit */
- #if kCompileWithQuitOnLongIdle
- sleepTicks = gIdleTimeToQuit / 60; /* convert ticks to seconds */
- sprintf ( tempString, "%d", sleepTicks );
- C2PStr ( tempString );
- GetDialogItem ( theDialog, kriIdleTimeBeforeQuit, &itemType, &item, &box );
- SetDialogItemText ( item, (StringPtr)tempString );
- #else
- //••• disable the dialog item and text item
- #endif
-
- /* drawing function for default button's outline */
- defaultButtonOutlineUPP = NewUserItemProc ( DialogDefaultButtonOutline );
- GetDialogItem ( theDialog, kDefaultButtonOutline, &itemType, &item, &box );
- SetDialogItem ( theDialog, kDefaultButtonOutline, itemType, (Handle)defaultButtonOutlineUPP, &box );
-
- dialogEventFilterUPP = NewModalFilterProc ( prefDialogEventFilter );
-
- /* initialize loop variables */
- itemHit = 0;
- dialogNotDone = true;
- dialogCancelled = false;
-
- /* now make it visible */
- dialogWindow = GetDialogWindow ( theDialog );
- ShowWindow ( dialogWindow );
- // SetPort ( theDialog );
-
- /* loop on modal dialog */
- do
- {
- ModalDialog ( dialogEventFilterUPP, &itemHit );
- switch ( itemHit )
- {
- case kDefaultButtonID :
- /* set the user login id */
- GetDialogItem ( theDialog, kriSleepTicks, &itemType, &item, &box );
- GetDialogItemText ( item, (StringPtr)sleepTicksString );
- if ( sleepTicksString[0] == nil )
- {
- SysBeep ( 30 );
- SelectDialogItemText ( theDialog, kriSleepTicks, nil, 32767 );
- }
- else
- {
- GetDialogItem ( theDialog, kriSleepTicksBusy, &itemType, &item, &box );
- GetDialogItemText ( item, (StringPtr)sleepTicksBusyString );
- if ( sleepTicksBusyString[0] == nil )
- {
- SysBeep ( 30 );
- SelectDialogItemText ( theDialog, kriSleepTicksBusy, nil, 32767 );
- }
- else
- {
- #if kCompileWithQuitOnLongIdle
- GetDialogItem ( theDialog, kriIdleTimeBeforeQuit, &itemType, &item, &box );
- GetDialogItemText ( item, (StringPtr)idleTimeQuitString );
- if ( idleTimeQuitString[0] == nil )
- {
- SysBeep ( 30 );
- SelectDialogItemText ( theDialog, kriIdleTimeBeforeQuit, nil, 32767 );
- }
- else
- {
- #endif
-
- dialogNotDone = false;
-
- #if kCompileWithQuitOnLongIdle
- }
- #endif
- }
- }
- break;
-
- case kCancelButtonID :
- dialogCancelled = true;
- dialogNotDone = false;
- break;
-
- case kriSleepTicks :
- case kriSleepTicksBusy :
- case kriIdleTimeBeforeQuit :
- default :
- break;
- }
- } while ( dialogNotDone );
-
- if ( !dialogCancelled )
- {
- /* Sleep Ticks */
- P2CStr ( (StringPtr)sleepTicksString );
- sleepTicks = atol ( sleepTicksString );
- PrefSaveSleepTicks ( sleepTicks );
- ProcessSleepSetDefault ( sleepTicks );
-
- /* Sleep Ticks Busy */
- P2CStr ( (StringPtr)sleepTicksBusyString );
- sleepTicks = atol ( sleepTicksBusyString );
- PrefSaveSleepTicksBusy ( sleepTicks );
- ProcessSleepSetBusy ( sleepTicks );
-
- ProcessResetSleep ();
-
- /* Idle Time to Quit */
- #if kCompileWithQuitOnLongIdle
- P2CStr ( (StringPtr)idleTimeQuitString );
- sleepTicks = atol ( idleTimeQuitString );
- sleepTicks *= 60; /* convert to seconds */
- PrefSaveIdleTimeToQuit ( sleepTicks );
- gIdleTimeToQuit = sleepTicks; //• this should use an accessor function instead of directly accessing the global
- #endif
- }
-
- DisposeRoutineDescriptor ( dialogEventFilterUPP );
- DisposeRoutineDescriptor ( defaultButtonOutlineUPP );
- DisposeDialog ( theDialog );
- SetPort ( savePort );
- } /* PrefDialog */
-
-
- /* This function responds to update, and activate events, and key presses.
- return and enter select the 'OK' button. Command-. and esc select 'Cancel'. */
- pascal Boolean
- prefDialogEventFilter ( DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
- {
- Boolean result;
- WindowPtr dialogWindow;
- char key;
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- long finalTicks;
-
- result = false;
-
- switch ( theEvent->what )
- {
- case updateEvt :
- dialogWindow = GetDialogWindow ( theDialog );
- if ( (WindowPtr)theEvent->message != dialogWindow )
- {
- doUpdateEvent ( theEvent );
- }
- break;
-
- case activateEvt :
- break;
-
- case keyDown :
- case autoKey :
- key = (char)( theEvent->message & charCodeMask );
- if ( ( key == (char)kReturnKey ) || ( key == (char)kEnterKey ) )
- {
- GetDialogItem ( theDialog, kDefaultButtonID, &itemType, &itemHandle, &itemRect );
- HiliteControl ( (ControlHandle)itemHandle, 0 );
- Delay ( (long)kVisualDelay, &finalTicks );
- HiliteControl ( (ControlHandle)itemHandle, 0 );
-
- *itemHit = (short)kDefaultButtonID;
- result = true;
- }
- else if ( ( key == (char)kEscapeKey ) ||
- ( (key == (char)kPeriodKey) && (theEvent->modifiers & cmdKey) ) )
- {
- GetDialogItem ( theDialog, kCancelButtonID, &itemType, &itemHandle, &itemRect );
- HiliteControl ( (ControlHandle)itemHandle, 0 );
- Delay ( (long)kVisualDelay, &finalTicks );
- HiliteControl ( (ControlHandle)itemHandle, 0 );
-
- *itemHit = (short)kCancelButtonID;
- result = true;
- }
- else if ( key < ' ' )
- {
- /* pass control characters */
- }
- else if ( (key < '0') || (key > '9') )
- {
- /* beep for non-digit chars */
- SysBeep ( 30 );
-
- // result = true;
- }
- break;
- }
-
- return ( result );
- } /* prefDialogEventFilter */
-
-
- #endif /* kCompileWithPreferencesDialog */
- /***** EOF *****/
-